var t, a = getApp(), e = require("../../../mymap/qqmap-wx-jssdk.min.js"), s = 0;

Page({
    data: {
        language: "",
        height: "",
        point: {
            lat: "",
            lng: ""
        },
        city: "",
        address: "",
        suggestionshow: !1,
        suggestion: [],
        classificationshow: !1,
        Locationshow: !1,
        commodityTitle: "",
        addgoodslistshow: !1,
        goodsdata: "",
        storeshow: !1,
        storedata: "",
        protocolshow: !1,
        photosize: 4194304,
        uploadUrl: "upload.uploadPic",
        catelist: [],
        catname: "",
        goodslistData: [],
        goodsisLoadMore: !0,
        goodsPage: 1,
        goodsTotal: 0,
        selectgoodsData: {},
        storelistData: [],
        storePage: 1,
        storeTotal: 0,
        storeisLoadMore: !0,
        selectstoreData: {},
        imgData: [],
        releasetitle: "",
        releasecontents: "",
        catid: "",
        releaseaddress: "",
        releasecity: "",
        releaselat: "",
        releaselnt: "",
        goodsId: "",
        storeId: "",
        checked: !0,
        agreementcontent: "",
        goods_open: 0,
        store_open: 0,
        circleId: 0,
        stickid: 0
    },
    onLoad: function(s) {
        var o = this;
        s.circleId && 0 != s.circleId && (this.data.circleId = s.circleId), s.stickId && 0 != s.stickId && (this.data.stickid = s.stickId, 
        this.getMicroCommentDetails()), a.globalData.qqmapkey ? (t = new e({
            key: a.globalData.qqmapkey
        }), this.poscurrent()) : a._getTemplate().then(function(s) {
            t = new e({
                key: a.globalData.qqmapkey
            }), o.poscurrent();
        }), this.getAgreement(), wx.getSystemInfo({
            success: function(t) {
                o.setData({
                    height: t.windowHeight - 70
                });
            }
        });
    },
    removeImg: function(t) {
        var a = t.currentTarget.dataset.index;
        this.data.imgData.splice(a, 1), this.setData({
            imgData: this.data.imgData
        });
    },
    poscurrent: function() {
        wx.getSetting({
            success: function(t) {
                t.authSetting["scope.userLocation"] || wx.authorize({
                    scope: "scope.userLocation",
                    success: function() {},
                    fail: function(t) {},
                    complete: function() {}
                });
            }
        }), this.getlocaldata();
    },
    getlocaldata: function() {
        var t = this;
        wx.getLocation({
            type: "gcj02",
            success: function(a) {
                var e = a.latitude, s = a.longitude;
                t._setaddressData({
                    lat: e,
                    lng: s
                });
            },
            fail: function(a) {
                "getLocation:fail auth deny" === a.errMsg && (console.log("当初用户拒绝，再次发起授权"), wx.openSetting({
                    success: function(a) {
                        a.authSetting["scope.userLocation"] ? (wx.showToast({
                            icon: "none",
                            title: "获取权限成功",
                            duration: 1500
                        }), t.getlocaldata()) : wx.showToast({
                            icon: "none",
                            title: "获取权限失败,定位需要用户授权",
                            duration: 1500
                        });
                    }
                }));
            }
        });
    },
    _setaddressData: function(e) {
        var s = this;
        t.reverseGeocoder({
            location: {
                latitude: e.lat,
                longitude: e.lng
            },
            sig: a.globalData.qqmapsig,
            success: function(t) {
                console.log(t), s.setData({
                    releaseaddress: t.result.address,
                    releasecity: t.result.address_component.city ? t.result.address_component.city : t.result.address_component.province,
                    releaselat: t.result.ad_info.location.lat,
                    releaselnt: t.result.ad_info.location.lng,
                    address_detail: "",
                    Locationshow: !1,
                    point: {
                        lat: t.result.ad_info.location.lat,
                        lng: t.result.ad_info.location.lng
                    },
                    city: t.result.address_component.city ? t.result.address_component.city : t.result.address_component.province,
                    address: t.result.address
                });
            },
            fail: function(t) {
                wx.showToast({
                    duration: 3e3,
                    title: t.message,
                    icon: "none"
                });
            },
            complete: function(t) {}
        });
    },
    onReady: function() {
        var t = wx.getStorageSync("langIndex");
        this.setData({
            language: t.en
        });
    },
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {},
    onShareTimeline: function() {},
    getAgreement: function() {
        var t = this, e = a.getNetAddresss("plugin.circle.frontend.circle.get-set");
        a._getNetWork({
            url: e,
            success: function(a) {
                var e = a.data;
                1 == e.result && t.setData({
                    goods_open: e.data ? e.data.good_show : 0,
                    store_open: e.data ? e.data.store_show : 0,
                    agreementcontent: e.data ? e.data.user_control : ""
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    onRead: function() {
        var t = this, e = a.getNetAddresss(this.data.uploadUrl);
        wx.chooseImage({
            count: 9,
            sizeType: [ "compressed" ],
            sourceType: [ "album", "camera" ],
            success: function(a) {
                var s = a.tempFilePaths;
                console.log(a);
                for (var o = 0; o < s.length; o++) wx.compressImage({
                    src: s[o],
                    quality: 60,
                    success: function(a) {
                        console.log(a);
                        var s = a.tempFilePath;
                        wx.uploadFile({
                            url: e,
                            filePath: s,
                            name: "file",
                            success: function(a) {
                                var e = JSON.parse(a.data);
                                if (1 == e.result) {
                                    var s = e.data.img_url, o = t.data.imgData;
                                    o.push(s), t.setData({
                                        imgData: o
                                    });
                                } else wx.showToast({
                                    icon: "none",
                                    title: e.msg,
                                    duration: 1500
                                });
                            }
                        });
                    },
                    fail: function(t) {},
                    complete: function(t) {}
                });
            }
        });
    },
    releasetitleInp: function(t) {
        var a = t.detail.value;
        this.setData({
            releasetitle: a
        });
    },
    releasecontentsInp: function(t) {
        var a = t.detail.value;
        this.setData({
            releasecontents: a
        });
    },
    getClassification: function() {
        this.setData({
            classificationshow: !0
        }), this.getClassificationData();
    },
    getClassificationData: function() {
        var t = this, e = {}, s = a.getNetAddresss("plugin.micro-communities.api.index.getCate");
        this.data.commodityTitle && (e.cName = this.data.commodityTitle), a._getNetWork({
            url: s,
            data: e,
            success: function(a) {
                var e = a.data;
                1 == e.result ? t.setData({
                    catelist: e.data
                }) : wx.showToast({
                    icon: "none",
                    title: e.msg,
                    duration: 1500
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    getReleaseAddress: function() {
        this.address_detail = "", this.setData({
            Locationshow: !0
        });
    },
    getGoodsMessage: function() {
        this.getGoodsMessageData(), this.setData({
            addgoodslistshow: !0
        });
    },
    getStoreMessage: function() {
        this.getStoreMessageData(), this.setData({
            storeshow: !0
        });
    },
    getStoreMessageData: function() {
        var t = this, e = a.getNetAddresss("plugin.circle.frontend.circle-invitation.search-store"), s = {};
        s.store_name = this.data.storedata, t.setData({
            storePage: 1
        }), a._getNetWork({
            url: e,
            data: s,
            success: function(a) {
                var e = a.data;
                1 == e.result ? t.setData({
                    storelistData: e.data.data,
                    storeTotal: e.data.last_page
                }) : wx.showToast({
                    icon: "none",
                    title: e.msg,
                    duration: 1500
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    getStoreMoreData: function() {
        var t = this;
        if (t.data.storeisLoadMore = !1, !(this.data.storePage >= this.data.storeTotal)) {
            this.data.storePage = this.data.storePage + 1;
            var e = a.getNetAddresss("plugin.circle.frontend.circle-invitation.search-store");
            a._getNetWork({
                url: e,
                data: {
                    page: t.data.storePage,
                    store_name: t.data.storedata
                },
                success: function(a) {
                    var e = a.data;
                    if (t.data.storeisLoadMore = !0, 1 == e.result) {
                        var s = e.data.data;
                        console.log(t.data.storelistData), t.setData({
                            storelistData: t.data.storelistData.concat(s)
                        });
                    } else t.data.storePage = t.data.storePage - 1, t.data.storeisLoadMore = !1;
                },
                fail: function(t) {
                    console.log(t);
                }
            });
        }
    },
    openProtocol: function() {
        this.setData({
            protocolshow: !0
        });
    },
    submitBtn: function() {
        if (0 == this.data.imgData.length) return a.tips("请上传图片");
        if (!this.data.releasetitle) return a.tips("请填写标题");
        if (!this.data.releasecontents) return a.tips("请添加正文");
        if (!this.data.releaseaddress) return a.tips("请选择地点");
        if ("1" != s) {
            s = 1;
            var t = {}, e = a.getNetAddresss("plugin.circle.frontend.circle-invitation.release");
            t.cat_id = this.data.catid, t.content = this.data.releasecontents, t.address = this.data.releaseaddress, 
            t.img = this.data.imgData, t.title = this.data.releasetitle, t.goods_id = this.data.goodsId, 
            t.store = this.data.storeId, t.city = this.data.releasecity, t.latitude = this.data.releaselat, 
            t.longitude = this.data.releaselnt, t.ylat = this.data.releaselat, t.ylng = this.data.releaselnt, 
            t.invitation_status = 1, t.circle_id = this.data.circleId, this.data.stickid && 0 != this.data.stickid && (t.invitation_id = this.data.stickid), 
            a._postNetWork({
                url: e,
                showToastIn: !1,
                data: t,
                success: function(t) {
                    s = 0;
                    var e = t.data;
                    if (a.tips(e.msg), 1 == e.result) {
                        try {
                            var o = getCurrentPages();
                            o[o.length - 3].updateView();
                        } catch (t) {
                            console.log(t);
                        }
                        wx.navigateBack({
                            delta: 2
                        });
                    }
                },
                fail: function(t) {
                    s = 0;
                }
            });
        } else console.log("重复请求");
    },
    protocolBtn: function(t) {
        var a = t.detail;
        this.setData({
            checked: a
        });
    },
    onClickLeft: function() {
        this.setData({
            classificationshow: !1
        });
    },
    onClickRight: function() {
        this.setData({
            classificationshow: !1
        });
    },
    commodityTitleClear: function() {
        this.setData({
            commodityTitle: ""
        });
    },
    commodityTitleInp: function(t) {
        var a = t.detail.value;
        this.setData({
            commodityTitle: a
        });
    },
    searchBtn: function() {
        this.getClassificationData();
    },
    selectCateId: function(t) {
        var a = t.currentTarget.dataset.id, e = t.currentTarget.dataset.name;
        this.setData({
            catid: a,
            catname: e,
            classificationshow: !1
        });
    },
    onAddressLeft: function() {
        this.setData({
            Locationshow: !1,
            suggestionshow: !1,
            address_detail: ""
        });
    },
    onAddressRight: function() {
        this.setData({
            Locationshow: !1,
            suggestionshow: !1,
            address_detail: ""
        });
    },
    addressinpbtn: function(t) {
        var a = t.detail.value;
        this.addressinp(a);
    },
    addressinp: function(e) {
        var s = this;
        e ? t.getSuggestion({
            keyword: e,
            sig: a.globalData.qqmapsig,
            success: function(t) {
                for (var a = [], e = 0; e < t.data.length; e++) a.push({
                    title: t.data[e].title,
                    id: t.data[e].id,
                    addr: t.data[e].address,
                    city: t.data[e].city,
                    district: t.data[e].district,
                    latitude: t.data[e].location.lat,
                    longitude: t.data[e].location.lng
                });
                s.setData({
                    suggestion: a,
                    suggestionshow: !0
                });
            },
            fail: function(t) {
                console.error(t);
            },
            complete: function(t) {}
        }) : this.setData({
            suggestionshow: !1
        });
    },
    backfill: function(t) {
        for (var a = t.currentTarget.id, e = 0; e < this.data.suggestion.length; e++) e == a && this.setData({
            releaseaddress: this.data.suggestion[e].addr,
            releasecity: this.data.suggestion[e].city,
            releaselat: this.data.suggestion[e].latitude,
            releaselnt: this.data.suggestion[e].longitude,
            address_detail: "",
            suggestionshow: !1,
            Locationshow: !1
        });
    },
    onAddGoodsLeft: function() {
        this.setData({
            addgoodslistshow: !1
        });
    },
    onAddGoodsRight: function() {
        this.setData({
            addgoodslistshow: !1
        });
    },
    goodsdataClear: function() {
        this.setData({
            goodsdata: ""
        });
    },
    goodsdataInp: function(t) {
        var a = t.detail.value;
        this.setData({
            goodsdata: a
        });
    },
    searchGoodBtn: function() {
        this.getGoodsMessageData();
    },
    getGoodsMessageData: function() {
        var t = this, e = {};
        e.goods_name = this.data.goodsdata;
        var s = a.getNetAddresss("plugin.circle.frontend.circle-invitation.search-goods");
        t.setData({
            goodsPage: 1
        }), a._getNetWork({
            url: s,
            data: e,
            success: function(a) {
                var e = a.data;
                1 == e.result ? t.setData({
                    goodslistData: e.data.data,
                    goodsTotal: e.data.last_page
                }) : wx.showToast({
                    icon: "none",
                    title: e.msg,
                    duration: 1500
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    getGoodMoreData: function() {
        var t = this;
        if (t.data.goodsisLoadMore = !1, !(this.data.goodsPage >= this.data.goodsTotal)) {
            this.data.goodsPage = this.data.goodsPage + 1;
            var e = a.getNetAddresss("plugin.circle.frontend.circle-invitation.search-goods");
            a._getNetWork({
                url: e,
                data: {
                    page: t.data.goodsPage,
                    goods_name: t.data.goodsdata
                },
                success: function(a) {
                    var e = a.data;
                    if (t.data.goodsisLoadMore = !0, 1 == e.result) {
                        var s = e.data.data;
                        t.setData({
                            goodslistData: t.data.goodslistData.concat(s)
                        });
                    } else t.data.goodsPage = t.data.goodsPage - 1, t.data.goodsisLoadMore = !1;
                },
                fail: function(t) {
                    console.log(t);
                }
            });
        }
    },
    setGoodsId: function(t) {
        var a = t.currentTarget.dataset.id;
        this.setData({
            goodsId: a
        });
        for (var e = {}, s = 0; s < this.data.goodslistData.length; s++) if (this.data.goodsId == this.data.goodslistData[s].id) {
            e.id = this.data.goodslistData[s].id, e.title = this.data.goodslistData[s].title, 
            e.thumb = this.data.goodslistData[s].thumb, e.price = this.data.goodslistData[s].price, 
            e.market_price = this.data.goodslistData[s].market_price, e.show_sales = this.data.goodslistData[s].show_sales;
            break;
        }
        this.setData({
            selectgoodsData: e,
            addgoodslistshow: !1
        });
    },
    storedataClear: function() {
        this.setData({
            storedata: ""
        });
    },
    storedataInp: function(t) {
        var a = t.detail.value;
        this.setData({
            storedata: a
        });
    },
    searchStoreBtn: function() {
        this.getStoreMessageData();
    },
    setStoreId: function(t) {
        var a = t.currentTarget.dataset.id;
        this.setData({
            storeId: a
        });
        for (var e = {}, s = 0; s < this.data.storelistData.length; s++) if (this.data.storeId == this.data.storelistData[s].id) {
            e.id = this.data.storelistData[s].id, e.thumb = this.data.storelistData[s].thumb, 
            e.store_name = this.data.storelistData[s].store_name, e.address = this.data.storelistData[s].address;
            break;
        }
        this.setData({
            selectstoreData: e,
            storeshow: !1
        });
    },
    protocolLeft: function() {
        this.setData({
            protocolshow: !1
        });
    },
    onStoreLeft: function() {
        this.setData({
            storeshow: !1
        });
    },
    onStoreRight: function() {
        this.setData({
            storeshow: !1
        });
    },
    getMicroCommentDetails: function() {
        var t = this, e = a.getNetAddresss("plugin.circle.frontend.circle-invitation.details"), s = {
            invitation_id: this.data.stickid
        };
        a._postNetWork({
            url: e,
            showToastIn: !1,
            data: s,
            success: function(a) {
                var e = a.data;
                if (1 == e.result) {
                    if (t.setData({
                        releasecontents: e.data.content,
                        releaseaddress: e.data.address
                    }), t.setData({
                        circleId: e.data.circle_id,
                        imgData: e.data.img || [],
                        releasetitle: e.data.title,
                        goodsId: e.data.goods_id,
                        releasecity: e.data.city,
                        releaselat: e.data.ylat,
                        releaselnt: e.data.ylng
                    }), e.data.goods && e.data.goods.length > 0) {
                        var s = e.data.goods[0];
                        t.setData({
                            storeId: s.id,
                            "selectgoodsData.id": s.id,
                            "selectgoodsData.title": s.title,
                            "selectgoodsData.thumb": s.thumb,
                            "selectgoodsData.price": s.price,
                            "selectgoodsData.market_price": s.market_price,
                            "selectgoodsData.show_sales": s.show_sales
                        });
                    }
                    if (e.data.store && e.data.store.length > 0) {
                        var o = e.data.store[0];
                        t.setData({
                            "selectstoreData.id": o.id,
                            "selectstoreData.thumb": o.thumb,
                            "selectstoreData.store_name": o.store_name,
                            "selectstoreData.address": o.address
                        });
                    }
                } else wx.showToast({
                    icon: "none",
                    title: e.msg,
                    duration: 1500
                });
            },
            fail: function(t) {}
        });
    }
});